# Replication Archive for: 
# Coppock, Alexander and Donald P. Green. 2020. 
# "Do Belief Systems Exhibit Dynamic Constraint?" 
# The Journal of Politics, Forthcoming.

rm(list = ls())

library(tidyverse)
library(xtable)
library(janitor)

study_2 <- read_rds("data/clean/study_2_lucid_clean.rds")
study_2_replication <-
  read_rds("data/clean/studies_1_2_replications_lucid_cleaned.rds")

study_2_stacked <-
  bind_rows(
    Original = dplyr::select(study_2, age_5, educ_5, pid_7, female),
    Replication = dplyr::select(study_2_replication, age_5, educ_5, pid_7, female),
    .id = "sample"
  ) %>%
  mutate(sample = factor(sample, levels = c("Original", "Replication")))


age_tab <-
study_2_stacked %>%
  tabyl(age_5, sample) %>%
  adorn_percentages(denominator = "col") %>%
  adorn_pct_formatting()

educ_tab <-
  study_2_stacked %>%
  tabyl(educ_5, sample) %>%
  adorn_percentages(denominator = "col") %>%
  adorn_pct_formatting()

pid_tab <-
  study_2_stacked %>%
  mutate(pid_7 = factor(pid_7, levels = 1:7,
                        labels = c("Strong Democrat",
                                   "Not very strong Democrat",
                                   "Lean Democrat",
                                   "Independent",
                                   "Lean Republican",
                                   "Not very strong Republican",
                                   "Strong Republican"))) %>%
  tabyl(pid_7, sample) %>%
  adorn_percentages(denominator = "col") %>%
  adorn_pct_formatting()


female_tab <-
  study_2_stacked %>%
  mutate(female = factor(female, levels = c(0, 1), labels = c("Male", "Female"))) %>%
  tabyl(female, sample) %>%
  adorn_percentages(denominator = "col") %>%
  adorn_pct_formatting()


n_tab <-
study_2_stacked %>%
  mutate(N = "N") %>%
  tabyl(N, sample)
  

xtable(age_tab, digits = 1) %>% 
  print.xtable(
  file = "../Drafts/tables/study_2_age.tex",
  include.rownames = FALSE,
  include.colnames = FALSE,
  only.contents = TRUE,
  hline.after = c()
)

xtable(educ_tab, digits = 1) %>% 
  print.xtable(
  file = "../Drafts/tables/study_2_educ.tex",
  include.rownames = FALSE,
  include.colnames = FALSE,
  only.contents = TRUE,
  hline.after = c()
)

xtable(pid_tab, digits = 1) %>% 
  print.xtable(
  file = "../Drafts/tables/study_2_pid.tex",
  include.rownames = FALSE,
  include.colnames = FALSE,
  only.contents = TRUE,
  hline.after = c()
)

xtable(female_tab, digits = 1) %>% 
  print.xtable(
  file = "../Drafts/tables/study_2_female.tex",
  include.rownames = FALSE,
  include.colnames = FALSE,
  only.contents = TRUE,
  hline.after = c()
)

xtable(n_tab, digits = 0) %>% 
  print.xtable(
  file = "../Drafts/tables/study_2_n.tex",
  include.rownames = FALSE,
  include.colnames = FALSE,
  only.contents = TRUE,
  hline.after = c()
)
